<?php
declare (strict_types = 1);

namespace app\admin\controller;

use addons\Builder\facade\Builder;
use think\response\Json;

//该文件为自动生成，请按需修改
class Binding extends Base
{
    //所有定义变量均为演示，请根据实际需求更改

    //主键，默认为id
    protected string $pk = 'id';

    //主模型
    protected string $model = '\app\common\model\Binding';

    //关联模型
    protected array $hasOne = ['user','admin'];

    public function index(): string
    {
        //构建表格数据
        return Builder::Table()
            ->addColumn(['field' => 'id', 'title' => '订单ID', 'width' => 90])
            ->addColumn(['field' => 'uid', 'title' => '业务员', 'modify' => '2,select,getUser', 'templet' => '<span>{{d.user?d.user.name:"暂无"}}</span>'])
            ->addColumn(['field' => 'aid', 'title' => '后台账户', 'modify' => '2,select,getAdmin', 'templet' => '<span>{{d.admin?d.admin.admin_name:"暂无"}}</span>'])
            ->addColumn(['fixed' => 'right', 'align' => 'center', 'toolbar' => '#barDemo', 'title' => '操作', 'width' => 200])
            ->addRightButtons(['edit' => ['size' => '50%'], 'delete'])
            ->addTopButtons(['add' => ['size' => '70%']])
            ->addSearch([
                ['title', '等级名称搜索', 'input,Like'],
            ],false)
            ->fetch();
    }

    public static function getUser(): array
    {
        return array_replace([0 => '暂无'], \app\common\model\User::where(['is_salesman'=>1,'delete_time'=>0])->column('name', 'id'));
    }

    public static function getAdmin(): array
    {
        return array_replace([0 => '暂无'], \app\common\model\Admin::where(['status'=>1,'delete_time'=>0])->column('admin_name', 'id'));
    }


    public function save($param = [], $model = false): Json
    {
        $param = $this->request->post();
        $Binding = \app\common\model\Binding::where(['uid'=>$param['uid'],'aid'=>$param['aid'],'delete_time'=>0])->count();
        if($Binding)
        {
            return json(['code' => -1, 'msg' => '不可重复绑定']);
        }
        return parent::save($param, $model); // TODO: Change the autogenerated stub
    }
}
